﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Windows.Forms;
using SedionFormDesigner.Serializer;
using SedionFormDesigner.Infrastructure;
using SedionFormDesigner.Serializer.BasicStruct;
using SedionFormDesigner.CusControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel;
using CommonLibrary.DataModel.Form;

namespace SedionFormDesigner
{
    public class SedinFormSerializeManager
    {
        public FormModel SerializeXmlToControl(XElement xml)
        {
            return FormSerializer.SerializeXmlToControl(xml);
        }

        #region XmlToHtml

        public String SerializeXmlToHtml(XElement xml)
        {
            return FormSerializer.SerializeXmlToHtml(xml).ToString();
        }

        public String SerializeXmlToHtml(XElement xml, SedionDesignUserInfo userInfo)
        {
            XmlFromModelList modelList = FormSerializer.SerializeXmlToHtml(xml);
            foreach (XmlFormModel form in modelList)
            {
                for (int i = 0; i < form.Count; i++)
                {
                    var item = form[i].Value;
                    if (item.Type == typeof(BD_TextBox))
                    {
                        form[i].Value = setBD_Text(item, userInfo);
                    }
                    else if (item.Type == typeof(BD_ComboBox))
                    {
                        form[i].Value = setBD_ComboBox(item, userInfo);
                    }
                    else if (item.Type == typeof(BD_DataGridView))
                    {
                        form[i].Value = setBD_DataGridView(item, userInfo);
                    }
                }
            }

            var res = modelList.ToString();
            return res;
        }

        private ElementModel setBD_Text(ElementModel item, SedionDesignUserInfo userInfo)
        {
            var value = String.Empty;
            var baseValue = " readonly='readonly' ";
            switch (item.Value)
            {
                case "绑定到部门":
                    value = userInfo.UserDept;
                    break;
                case "绑定到发起人":
                    value = userInfo.UserName;
                    break;
                case "绑定到日期":
                    value = userInfo.Date;
                    break;
                case "绑定到编号":
                    value = userInfo.UserID;
                    break;
                case "绑定到职位":
                    value = userInfo.UserDuty;
                    break;
                case "不绑定":
                    baseValue = String.Empty;
                    break;
            }

            item.Value = value;
            item.AddBaseValue(baseValue);
            return item;
        }

        private ElementModel setBD_ComboBox(ElementModel item, SedionDesignUserInfo userInfo)
        {
            var value = String.Empty;
            var result = String.Empty;
            List<SqlParameter> parameterList;
            DataTable dt;

            if (item.ValueType == "SQL")
            {
                value = userInfo.UserName;
                parameterList = new List<SqlParameter>();
                parameterList.Add(new SqlParameter("@userid", userInfo.UserID));
                parameterList.Add(new SqlParameter("@deptid", userInfo.DeptID));
                parameterList.Add(new SqlParameter("@dutyid", userInfo.DutyID));
                parameterList.Add(new SqlParameter("@date", userInfo.Date));
                dt = CommonLibrary.DB.DBExecuteUtil.querySqlTable(item.Value, parameterList.ToArray());
                result = getComboValue(dt);
            }
            if (result != string.Empty)
                item.Value = FormSerializer.ProduHtmlSelectOption(result);
            return item;
        }

        private String getComboValue(DataTable dt)
        {
            var result = String.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
                result = String.Concat(result, dt.Rows[i][1], ',', dt.Rows[i][1], ';');
            return result;
        }

        private ElementModel setBD_DataGridView(ElementModel item, SedionDesignUserInfo userInfo)
        {
            if (item.ValueType == "SQL")
            {
                List<SqlParameter> parameterList = new List<SqlParameter>();
                parameterList.Add(new SqlParameter("@userid", userInfo.UserID));
                parameterList.Add(new SqlParameter("@deptid", userInfo.DeptID));
                parameterList.Add(new SqlParameter("@dutyid", userInfo.DutyID));
                parameterList.Add(new SqlParameter("@date", userInfo.Date));
                var dt = CommonLibrary.DB.DBExecuteUtil.querySqlTable(item.Value, parameterList.ToArray());
                item.Value = FormSerializer.ProduHtmlTable(dt);
            }
            return item;
        }

        public String SerializeXmlToHtml(XElement xml, List<FormValueEntities> formValueEntity, DataTable dt_spjg)
        {
            XmlFromModelList modelList = FormSerializer.SerializeXmlToHtml(xml, formValueEntity, dt_spjg);

            return modelList.ToString();
        }

        #endregion

        #region XmlToEnty

        public FormEntity SerializeXmlToFormEntity(XElement xml)
        {
            return FormSerializer.SerializeXmlToFormEntity(xml);
        }

        #endregion

    }
}
